import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyArrayList {
    public int[] elem;
    public int usedSize;
    private static final int DEFAULT_SIZE = 4;

    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
    }

    public void display(){
        for(int i=0;i<this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    private boolean isFull(){
        return this.usedSize==this.elem.length;//判断是否已满
    }

    private boolean isEmpty() {return this.usedSize==0;}

    private boolean checkPosInAdd(int pos){
        if(pos<0||pos>this.usedSize){
            System.out.println("位置不合法");
            return false;
        }
        return true;
    }

    public void add(int val){//默认尾插
        if(isFull()){//已满则进行扩容
            this.elem= Arrays.copyOf(this.elem,2*elem.length);
        }
        this.elem[this.usedSize]=val;
        this.usedSize++;
    }

    public void add(int pos,int val){
        if(!checkPosInAdd(pos)){//判断加入下标合法性
            throw new MyArrayListIndexException("添加位置不合法！");
        }
        if(isFull()){
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        for(int i=this.usedSize-1;i>=pos;i--){//挪动数据
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=val;//插入数据
        this.usedSize++;
    }

    public boolean contains(int val){
        for(int i=0;i<this.usedSize;i++){
            if(this.elem[i]==val){
                return true;
            }
        }
        return false;
    }

    public boolean checkPosInGet(int pos){
        if(pos<0||pos>=this.usedSize){
            return false;
        }
        return true;
    }

    public int indexOf(int toFind) {// 查找某个元素对应的位置
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public int get(int pos){//获取pos位置元素
        if(!checkPosInGet(pos)){
            throw new MyArrayListIndexException("获取下标不合法！");
        }
        return this.elem[pos];
    }

    public void remove(int key){
        if(isEmpty()){
            throw new MyArrayListEmptyException("顺序表为空！");
        }
        int index=indexOf(key);
        if(index==-1){
            throw new MyArrayListIndexException("该元素不存在！");
        }
        else{
            for(int i=index;i<this.usedSize-1;i++){
                this.elem[i]=this.elem[i+1];
            }
        }
        this.usedSize--;
        // this.elem[usedSize] = null; 如果是引用类型 这里需要置为空
    }


    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    public void clear(){
        /*
        如果是引用数据类型 得一个一个置为空
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = null;
        }
        this.usedSize = 0;
        */
        this.usedSize=0;
    }

    public void set(int pos,int value){
        if(!checkPosInGet(pos)){
            throw new MyArrayListIndexException("下标位置不合法！");
        }
        this.elem[pos]=value;
    }
}

